package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import com.qf.model.entity.Examprojecttable;
import com.qf.service.ExamprojecttableService;

import java.util.ArrayList;
import java.util.List;

public class ExamprojecttableListener extends AnalysisEventListener<Examprojecttable> {


    // 用于存放临时的SysUser列表
    private List<Examprojecttable> examprojecttableList = new ArrayList<>();
    // 配置每次批量导入5条
    private static  final  int BATCH_COUNT=5;

    private ExamprojecttableService examprojecttableService; // 构建时传递studentService

    public ExamprojecttableListener() {
    }

    public ExamprojecttableListener(ExamprojecttableService examprojecttableService) {
        this.examprojecttableService = examprojecttableService;
    }


    @Override
    public void invoke(Examprojecttable examprojecttable, AnalysisContext analysisContext) {
        examprojecttableList.add(examprojecttable);

        if (examprojecttableList.size()>= BATCH_COUNT){
            // 批量导入
            examprojecttableService.insertBatch(examprojecttableList);
            // 清空
            examprojecttableList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 最后也要批量导入
        if (examprojecttableList.size()>0){
            examprojecttableService.insertBatch(examprojecttableList);
            examprojecttableList.clear();
        }
    }
}
